#------------------------------------------------------------------------------
# Import libraries
#------------------------------------------------------------------------------

rm(list=ls())
library(LalRUtils)
libreq(tidyverse, data.table, zoo, tictoc, fst, fixest, PanelMatch, patchwork,
       rio, magrittr, janitor, did, panelView, ggiplot, tictoc, binsreg, interflex)
set.seed(42)
theme_set(lal_plot_theme())

#------------------------------------------------------------------------------



#------------------------------------------------------------------------------
# Define Paths
#------------------------------------------------------------------------------

# R studio
setwd( dirname(rstudioapi::getActiveDocumentContext()$path) )
# R default : unccoment if you use default R
# setwd(getSrcDirectory(function(){})[1])
#------------------------------------------------------------------------------



#------------------------------------------------------------------------------
# Load Data
#------------------------------------------------------------------------------

vcf <- fread("vcf_data_complete.csv", sep = ",")
setnames( vcf, "d", "D")
vcf_data <- copy( vcf )
gfc <- fread("gfc_dta.csv", sep = ",")

#------------------------------------------------------------------------------



#------------------------------------------------------------------------------

gfc = gfc[pref == 1] # subset to primary sample

# %% data prep for interflex
gfc[, `:=`(id_f   = as.factor(village),
           sty_f  = as.factor(styear),
           D_f    = as.factor(D)
)]


# %% summary table - GFC
sumvars = c('def_ha', 'sch', 'pref_mean')
labs = c('Deforested Area (Hectares)',
         'Scheduled Status', 'Ex-ante forest cover in 2000 (ex-ante)'
)
summvars = c('notNA(x)', 'mean(x)', 'sd(x)', 'min(x)', 'pctile(x)[25]', 
             'median(x)', 'pctile(x)[75]', 'max(x)')

st(gfc[pref == 1, ..sumvars], summ = summvars,
   factor.percent = FALSE, factor.counts = FALSE,
   labels = labs, 
   title = "Summary Statistics (GFC Data) - above 2 percent forest cover in 2000",
   file = "appendix_tablea4.tex", out = 'latex')

#------------------------------------------------------------------------------

